﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SmsUI.DataBase;
using SMSCore;
using System.Threading;
using SmsUI.N77;

namespace SmsUI
{
    public partial class SetManagerForm : DevExpress.XtraEditors.XtraUserControl
    {
        smsdbDataSet.tb_SetDataTable tb = null;
        public SetManagerForm()
        {
            InitializeComponent();

            tb = DbRules.Instance.GetSetInfo();
            this.setGrid.DataSource = tb;
            this.Load += new EventHandler(SetManagerForm_Load);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = 50;
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            InitData();
            timer.Stop();
        }
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        void SetManagerForm_Load(object sender, EventArgs e)
        {


        }

        private void InitData()
        {
            //this.setGrid.DataSource = null;

            this.lbComPorts.Text += ComHelper.Instance.GetAvailComPortsString();
            //Thread t = new Thread(new ThreadStart(DetectAllUsePort));
            //t.Start();

            DetectAllUsePort();

        }
        private void DetectAllUsePort()
        {
            //SetManagerForm sendListForm = ((SetManagerForm)FormManager.Instance.GetForm("SetManagerItem"));
            //sendListForm.Invoke(new System.Windows.Forms.MethodInvoker(() =>
            //{
            foreach (smsdbDataSet.tb_SetRow row in tb)
            {
                if (row.State == "启用")
                {
                    row.State = "正在检测";
                    Thread.Sleep(10);
                    if (ComHelper.Instance.CheckComPorts(row.PortNum, row.BTL))
                    {
                        row.State = "启用";
                    }
                    else
                    {
                        row.State = "无法启用";
                    }
                }
            }
            //}));

        }
        public void ModifyDeviceState(string comPort, string state)
        {
            if (this.InvokeRequired)
            {
                foreach (smsdbDataSet.tb_SetRow row in tb)
                {
                    if (row.PortNum == comPort)
                    {
                        this.BeginInvoke(new MethodInvoker(() =>
                        {
                            row.HiddenState = state;
                        }));
                    }
                }
            }
            else
            {
                foreach (smsdbDataSet.tb_SetRow row in tb)
                {
                    if (row.PortNum == comPort)
                    {

                        row.HiddenState = state;

                    }
                }
            }
        }
        public void EnableSet()
        {
            if (!SmsHelper.SmsHelper.Instance.AllowModifyComPortState())
            {
                if (XtraMessageBox.Show("发送中修改设备状态将会停止发送，是否仍修改？", "注意", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    FormManager.Instance.MainForm.ServiceStop();
                }
                else
                {
                    return;
                }
            }

            int[] selectRows = this.gridView1.GetSelectedRows();
            for (int i = 0; i < selectRows.Length; i++)
            {
                int currRowID = selectRows[i];
                smsdbDataSet.tb_SetRow row = tb[currRowID];
                //if (string.IsNullOrEmpty(row.PortNum) || string.IsNullOrEmpty(row.BTL))
                //{
                //    // DevExpress.XtraEditors.XtraMessageBox.Show("请先设置端口号和波特率再启用端口!");
                //    row.State = "无法启用";
                //    continue;
                //}

                if (row.State == "停用")
                {
                    row.State = "正在检测";

                    if (ComHelper.Instance.CheckComPorts(row.PortNum, row.BTL))
                    {

                        row.State = "启用";
                        if (DbRules.Instance.GetStartPortCount() > CheckDog.PortCount)
                        {
                            row.State = "停用";
                            XtraMessageBox.Show("启用的端口数不能大于加密狗授权的数量！");
                            break;

                        }
                    }
                    else
                    {
                        row.State = "无法启用";
                    }
                }
                else
                {
                    if (row.State == "启用" || row.State == "无法启用")
                    {
                        //SmsHelper.SmsHelper.Instance.RemoveGateway(row.PortNum);
                    }
                    row.State = "停用";
                }
            }
            DbRules.Instance.UpdateSetInfo();
            tb.AcceptChanges();
        }
        private void btnEnableSet_Click(object sender, EventArgs e)
        {
            EnableSet();

        }
        private void AysnEnableSet()
        {

        }
        private void setGrid_DoubleClick(object sender, EventArgs e)
        {
            SetModifyForm form = new SetModifyForm(this.gridView1.GetSelectedRows());
            //form.Parent = this;
            form.ShowDialog();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            SetModifyForm form = new SetModifyForm(this.gridView1.GetSelectedRows());
            //  form.Parent = this;
            form.ShowDialog();
        }

        private void btnBatchModify_Click(object sender, EventArgs e)
        {
            BatchModifySetForm form = new BatchModifySetForm();

            form.ShowDialog();
        }

        private void btnAllStart_Click(object sender, EventArgs e)
        {
            this.gridView1.SelectAll();
            EnableSet();
            this.gridView1.ClearSelection();
        }

        private void btnModifySmsNum_Click(object sender, EventArgs e)
        {
            InputNum win = new InputNum("中心号码：", ConfigManager.GetValue("smsNum"));
            win.ShowDialog();
            if (win.DialogResult != DialogResult.OK)
            {
                return;
            }
            string smsNum = win.PhoneNum;
            ConfigManager.SetValue("smsNum", smsNum);
        }

        private void btnDial_Click(object sender, EventArgs e)
        {
            if (SmsHelper.SmsHelper.Instance.IsCallFlag)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请停止号码真经检测后再拨打电话！");
                return;
            }
            if (SmsHelper.SmsHelper.Instance.IsSendFlag)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请停止发送短信再拨打电话！");
                return;
            }


            InputNum win = new InputNum("请输入号码：", "10086");

            win.ShowDialog();
            int[] sele = this.gridView1.GetSelectedRows();
            string[] sportNum = new string[sele.Length];
            string[] sbtl = new string[sele.Length];
            string[] sMatype = new string[sele.Length];
            bool hasPort = false;
            if (win.DialogResult == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(win.PhoneNum))
                {

                    for (int i = 0; i < sele.Length; i++)
                    {
                        int currRowID = sele[i];
                        smsdbDataSet.tb_SetRow row = tb[currRowID];
                        if (string.IsNullOrEmpty(row.PortNum))
                        {

                            XtraMessageBox.Show("请选择Com口!");
                            continue;

                        }
                        hasPort = true;
                        sportNum[i] = row.PortNum;
                        sbtl[i] = row.BTL;
                        sMatype[i] = row.MaType;

                    }
                    if (hasPort)
                    {
                        SmsHelper.SmsHelper.Instance.DialNum(sportNum, sbtl, sMatype, win.PhoneNum);
                    }

                }
            }

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            OtherSettingForm sf = new OtherSettingForm();
            sf.ShowDialog();
        }
    }
}
